package com.zlc.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlc.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.zlc.gulimall.product.dao.AttrGroupDao;
import com.zlc.gulimall.product.dao.CategoryDao;
import com.zlc.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zlc.gulimall.product.entity.AttrGroupEntity;
import com.zlc.gulimall.product.entity.CategoryEntity;
import com.zlc.gulimall.product.vo.AttrRespVo;
import com.zlc.gulimall.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlc.common.utils.PageUtils;
import com.zlc.common.utils.Query;

import com.zlc.gulimall.product.dao.AttrDao;
import com.zlc.gulimall.product.entity.AttrEntity;
import com.zlc.gulimall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Resource
    AttrAttrgroupRelationServiceImpl attrAttrgroupRelationService;
    @Resource
    CategoryDao categoryDao;
    @Resource
    AttrGroupDao attrGroupDao;
    @Resource
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Resource
    AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCatelogId(Map<String, Object> params, Long catelogId,Long attrType) {
        IPage<AttrEntity> page = null;
        //不带ID默认查询所有
        if(catelogId == 0){
            page = this.page(
                    new Query<AttrEntity>().getPage(params),
                    new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getAttrType,attrType)
            );
        }else{
            //带有ID
             page = this.page(new Query<AttrEntity>().getPage(params),
                    new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getCatelogId, catelogId).eq(AttrEntity::getAttrType,attrType));
        }
        //处理数据，让数据附带分类名 和 分组名
        List<AttrEntity> records = page.getRecords();
        List<AttrRespVo> list = records.stream().map((attrEntity) -> {
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(attrEntity, attrRespVo);
            //获取分组名
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationDao
                    .selectOne(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                            .eq(AttrAttrgroupRelationEntity::getAttrId, attrEntity.getAttrId()));
            Long attrGroupId = attrAttrgroupRelationEntity.getAttrGroupId();
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);
            //获取分类名
            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());

            attrRespVo.setAttrGroupId(attrGroupId);
            attrRespVo.setCatelogName(categoryEntity.getName());
            attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
            return attrRespVo;
        }).collect(Collectors.toList());

        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(list);
        return pageUtils;
    }

    @Transactional
    @Override
    public void saveAttr(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo,attrEntity);
        this.save(attrEntity);

        AttrAttrgroupRelationEntity relation = new AttrAttrgroupRelationEntity();
        relation.setAttrId(attrEntity.getAttrId());
        relation.setAttrGroupId(attrVo.getAttrGroupId());
        attrAttrgroupRelationService.save(relation);
    }

    @Override
    public List<AttrEntity> getRelationAttrs(Long attrGroupId) {

        List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationDao.selectList(
                new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupId)
        );

        List<AttrEntity> result = list.stream().map((item) -> {
            Long attrId = item.getAttrId();
            AttrEntity attrEntity = attrDao.selectById(attrId);
            return attrEntity;
        }).collect(Collectors.toList());

        return result;
    }

    @Override
    public List<AttrEntity> getByCategoryId(Long catalogId) {
        List<AttrEntity> list = this.list(
                new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getCatelogId, catalogId)
        );
        return list;
    }

    @Override
    public List<Long> selectSearchAttrs(List<Long> attrIds) {
        List<Long> attrs = attrDao.selectSearchAttrs(attrIds);

        return attrs;
    }

}